<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
<body>
<?
function request_cache($url, $dest_file, $timeout=43200) {
  if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
    $stream = fopen($url,'r');
    $tmpf = tempnam('/tmp','YWS');
    file_put_contents($tmpf, $stream);
    fclose($stream);
    rename($tmpf, $dest_file);
  }
}

function yahoo_geo($location) {
  $q = 'http://api.local.yahoo.com/MapsService/V1/geocode';
  $q .= '?appid=YD-9G7bey8_JXxQP6rxl.fBFGgCdNjoDMACQA--&location='.$location;
  $tmp = '/tmp/yws_geo_'.md5($q);
  request_cache($q, $tmp, 43200);
  libxml_use_internal_errors(true);
  $xml = simplexml_load_file($tmp); 
  $ret['precision'] = (string)$xml->Result['precision'];
  foreach($xml->Result->children() as $key=>$val) {
    if(strlen($val)) $ret[(string)$key] =  (string)$val;
  } 
  return $ret;
}

function geonames_geo($location) {
  $q = 'http://ws.geonames.org/search';
  $q .= '?q='.$location;
  $tmp = '/tmp/yws_geo_'.md5($q);
  request_cache($q, $tmp, 43200);
  libxml_use_internal_errors(true);
  $xml = simplexml_load_file($tmp); 
  if(intval($xml->totalResultsCount) >= 1){
    $ret = $xml->geoname[0]->geonameId[0];
  } 
  return $ret;
}

function dbpedia_uri($location) {
  $q = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=&MaxHits=1&QueryString=Springer';
  $q .= '?q='.$location;
  $tmp = '/tmp/yws_geo_'.md5($q);
  request_cache($q, $tmp, 43200);
  libxml_use_internal_errors(true);
  $xml = simplexml_load_file($tmp); 
  if(intval($xml->totalResultsCount) >= 1){
    $ret = $xml->geoname[0]->geonameId[0];
  } 
  return $ret;
}
?>

<?
// Get geonames XML Info
$a = geonames_geo($_GET['l']);
echo $a;
?>
</body>
</html>